/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.Controller;

import com.Model.Campo;
import com.Model.Repositories.CampoRepository;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Fellipe
 */
@ManagedBean
@ViewScoped
public class Campos {

    private Campo campo;
    private List<Campo> campos;

    public void delete() {
        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        int id = Integer.parseInt(params.get("chave"));
        new CampoRepository().delete(id);
    }

    public void update() {
        new CampoRepository().update(this.campo);
    }

    public void create() {
        new CampoRepository().create(this.campo);
    }

    public List<Campo> getCampos() {
        if (this.campos == null) {
            this.campos = new CampoRepository().readNomes();
        }
        return this.campos;
    }

    public Campo getCampo() {
        if (this.campo == null) {
            Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
            int id = Integer.parseInt(params.get("chave"));
            this.campo = new CampoRepository().read(id);
        }
        return this.campo;
    }
}
